/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Company;
import Beans.DriverBean;
import Processes.Navigate;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import radio.entities.AccountDetails;
import radio.entities.Accounts;
import radio.entities.City;
import radio.sessions.AccountDetailsFacade;
import radio.sessions.AccountsFacade;
import radio.sessions.CityFacade;
import radio.sessions.CompanyFacade;

/**
 *
 * @author Kim
 */
public class PreCompany {
    @EJB
    private CityFacade cityFacade;

    public PreCompany(CompanyFacade companyFacade, AccountDetailsFacade accountDetailsFacade, AccountsFacade accountsFacade, Company com,CityFacade cityFacade) {
        this.companyFacade = companyFacade;
        this.accountDetailsFacade = accountDetailsFacade;
        this.accountsFacade = accountsFacade;
        this.cityFacade=cityFacade;
        this.com = com;
    }
    @EJB
    private CompanyFacade companyFacade;
    @EJB
    private AccountDetailsFacade accountDetailsFacade;
    @EJB
    private AccountsFacade accountsFacade;
private Company com;
private String Newpwd,Confirmnew,Oldpwd;
private int citySearch;
private ArrayList<DriverBean> lst;
private DriverBean SelectedDriver;

    public DriverBean getSelectedDriver() {
        return SelectedDriver;
    }

    public void setSelectedDriver(DriverBean SelectedDriver) {
        this.SelectedDriver = SelectedDriver;
    }
    

    public int getCitySearch() {
        return citySearch;
    }

    public void setCitySearch(int citySearch) {
        this.citySearch = citySearch;
    }
    public String getConfirmnew() {
        return Confirmnew;
    }

    public void setConfirmnew(String Confirmnew) {
        this.Confirmnew = Confirmnew;
    }

    public String getNewpwd() {
        return Newpwd;
    }

    public void setNewpwd(String Newpwd) {
        this.Newpwd = Newpwd;
    }

    public String getOldpwd() {
        return Oldpwd;
    }

    public void setOldpwd(String Oldpwd) {
        this.Oldpwd = Oldpwd;
    }
    public Company getCom() {
        return com;
    }

    public void setCom(Company com) {
        this.com = com;
    }
public String gCityName(int accId) {        
        AccountDetails detail=accountDetailsFacade.find(accId);
        String cit=detail.getCityId().getCityName();        
        return cit;
    }
public String getgenderName(boolean gender){
        if(gender)
            return "Male";
        else return "Female";
    }
public String gTypeName(){
    Accounts acc=accountsFacade.find(this.com.getAccId());
    String type=acc.getTypeId().getTypeName();
    return type;
}
public String gStatus(){    
    if(accountsFacade.find(this.com.getAccId()).getStatus()){
        return "Actived";
    }else 
        return "InActived";
    }

public int gDayRemain(){
    Date end=accountsFacade.find(this.com.getAccId()).getEndDate();
    Date now=new Date();
    int remain=end.compareTo(now);
    return remain;
}
public String gEndDate(){
    Date end=accountsFacade.find(this.com.getAccId()).getEndDate();
    String date=DateFormat.getDateInstance().format(end);          
    return date;
}
    /** Creates a new instance of PreCompany */
    public PreCompany() {
    }
    public String edit(){        
        if(this.getNewpwd().equals("")&&this.getOldpwd().equals("")&&this.getConfirmnew().equals("")){            
            editAccwopassword();
            Navigate nav=(Navigate) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("navigate");
            nav.setPrecom("./Home.xhtml");
            return "CompanyPage.xhtml";
        }
        else if(!this.getOldpwd().equals("") &&!this.com.getPwd().equals(this.getOldpwd())){
            FacesContext cxt=FacesContext.getCurrentInstance();
            cxt.addMessage("EditForm:old",new FacesMessage("Old Password does not match"));
            this.setNewpwd(null);
            this.setConfirmnew(null);
            this.setOldpwd(null);            
            return "";
        }
        else if(!this.getConfirmnew().equals(this.getNewpwd())){
            FacesContext cxt=FacesContext.getCurrentInstance();
            cxt.addMessage("EditForm:confirm",new FacesMessage("Confirm Password does not match"));
            this.setOldpwd(null);
            this.setNewpwd(null);
            this.setConfirmnew(null);
            return "";
        }else{          
            editAcc();
            Navigate nav=(Navigate) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("navigate");
            nav.setPrecom("./Home.xhtml");
            return "CompanyPage.xhtml";
        }
    }
    public void editAccwopassword(){
            
            Accounts acc=accountsFacade.findbyId(this.com.getAccId()); 
            AccountDetails detail=acc.getAccountDetails();
//            java.util.Date dd=new java.util.Date();
//            
//            Date d=new Date(new java.util.Date().getTime());                        
            detail.setAddress(this.com.getAddress());
            detail.setContactPerson(this.com.getContact());
            detail.setEmail(this.com.getEmail());
            detail.setMobile(this.com.getMobile());
            detail.setTelephone(this.com.getTele());            
            City city=cityFacade.find(this.com.getCityId());
            detail.setCityId(city);    
            
//            acc.setPwd(this.getNewpwd());            
            acc.setGender(this.com.isGender());
//            accountDetailsFacade.edit(detail);
            radio.entities.Company compa=acc.getCompany();
            compa.setCompanyName(this.com.getCompanyname());
            compa.setDesignation(this.com.getDesignation());
            compa.setFax(this.com.getFax());
            accountsFacade.edit(acc);
//            this.com.setPwd(this.getNewpwd());
            System.out.println("Da edit");
            this.setOldpwd(null);
            this.setNewpwd(null);
            this.setConfirmnew(null);
    }
    public void editAcc(){
            
            Accounts acc=accountsFacade.findbyId(this.com.getAccId()); 
            AccountDetails detail=acc.getAccountDetails();
//            java.util.Date dd=new java.util.Date();
//            
//            Date d=new Date(new java.util.Date().getTime());                        
            detail.setAddress(this.com.getAddress());
            detail.setContactPerson(this.com.getContact());
            detail.setEmail(this.com.getEmail());
            detail.setMobile(this.com.getMobile());
            detail.setTelephone(this.com.getTele());            
            City city=cityFacade.find(this.com.getCityId());
            detail.setCityId(city);    
            
            acc.setPwd(this.getNewpwd());            
            acc.setGender(this.com.isGender());
//            accountDetailsFacade.edit(detail);
            
            accountsFacade.edit(acc);
            this.com.setPwd(this.getNewpwd());
            this.setOldpwd(null);
            this.setNewpwd(null);
            this.setConfirmnew(null);
    }
    public void searchdriver(){
        List<Accounts> arr=accountsFacade.SearchDriver(this.citySearch);
        ArrayList<DriverBean> lstte=new ArrayList<DriverBean>();
        if(arr!=null){
        for (int i = 0; i < arr.size(); i++) {
            Accounts acc = arr.get(i);
            DriverBean temp=new DriverBean(acc.getUid(), acc.getPwd(), acc.getImg(), acc.getAccountDetails().getContactPerson(), acc.getAccountDetails().getMobile(), acc.getAccountDetails().getTelephone(), acc.getAccountDetails().getAddress(), acc.getAccountDetails().getEmail(), acc.isGender(), acc.getStatus(), acc.getAccId(), acc.getTypeId().getTypeId(), acc.getAccountDetails().getCityId().getCityId(), acc.getDriver().getExperience(), acc.getDriver().getDesciption());            
            lstte.add(temp);
        }
        }
        this.setLst(lstte);
        
        Navigate nav=(Navigate) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("navigate");
            nav.setPrecom("./ShowDriver.xhtml");            
    }

    public ArrayList<DriverBean> getLst() {
        return lst;
    }

    public void setLst(ArrayList<DriverBean> lst) {
        this.lst = lst;
    }
}
